const PSD = require('psd');
const fs = require('fs-extra');

//psd相关接口方法测试
const test = function test(psd) {
    const psdTree = psd.tree();
    const psdTreeExport = psd.tree().export();

    console.log(psd);
    console.log(psdTree);
    console.log(psdTreeExport);

};
//图层相关测试
const testLayerInfo = function testLayerInfo(node) {
    let style;
    let left = node.layer.left;
    let top = node.layer.top;
    let width = node.layer.width;
    let height = node.layer.height;
    let name = node.layer.name;
    let text = '';
    let img = `./images/${index}_${node.layer.name}.png`;

    if (node.layer.typeTool) {//文本
        // console.log(node.layer.typeTool().export())//输出文本样式
        style = node.layer.typeTool().export();
        text = style.value;
    }
    debugger;
};


const getLayerNodes = (function xx() {//eslint提示，这里要一个名字，还是不够只能，自执行的代码基本不会用到名字
    let nodes = [];
    const searchForImage = (node, i) => {
        if (node && node.hasChildren()) {
            return searchForImage(node.childeren, i + 1);
        } else if (node) {
            nodes.push(node);
            return;
        }
    };
    return function _getLayerNodes(psd) {
        nodes = [];
        psd.tree().descendants().forEach((node) => {
            return searchForImage(node, 0);
        });
        return nodes;
    };
}());

// 获取可见图层
const getDetailVisibleNodes = (function xxx() {
    let arrLayers = [];

    const walkTreeExport = function walkTreeExport(arr, depth = 0) {
        let spaces = new Array(depth);
        spaces.fill('  ');
        for (let i = 0; i < arr.length; i++) {
            const item = arr[i];
            if (!item.visible()) {//图层是否隐藏
                continue;
            }
            if (item.name === '2') {
                debugger;
            }
            if (item.constructor.name === "Group") {
                walkTreeExport(item._children, depth + 1);
            } else {
                arrLayers.push(item);
            }
        }
    };

    return function _getDetailVisibleNodes(psd) {
        // test(psd);
        const psd_tree = psd.tree();
        arrLayers = [];
        walkTreeExport(psd_tree._children);
        return arrLayers;
    };
}());

// 获取可见图层
const getVisibleNodes = (function fn() {
    let arrLayers = [];

    const walkTreeExport = function walkTreeExport(arr, depth = 0) {
        const spaces = new Array(depth);
        spaces.fill('  ');
        for (let i = 0; i < arr.length; i++) {
            const item = arr[i];
            if (!item.visible) {//图层是否隐藏
                continue;
            }

            if (item.children) {
                walkTreeExport(item.children, depth + 1);
            } else {
                arrLayers.push(item);
            }
        }
    };

    return function _getVisibleNodes(psd) {
        // test(psd);
        const psdTreeExport = psd.tree().export();
        arrLayers = [];
        walkTreeExport(psdTreeExport.children);
        return arrLayers;
    }
}());

// 获取图层树结构
const getStructure = (function fn() {
    let json = {};
    const walkTreeExport = function walkTreeExport(arr, depth = 0, parentIsVisible = true, parent = json) {
        let spaces = new Array(depth);
        for (var i = 0; i < arr.length; i++) {
            let item = arr[i];
            let name = item.name;
            if (item.visible && parentIsVisible) {//图层是否隐藏
                spaces.fill('  ');
            } else {
                spaces.fill('**');
                name = "****" + name;
            }

            if (item.children) {
                // console.log(spaces.join('') + item.name);
                parent[name] = {}
                walkTreeExport(item.children, depth + 1, item.visible && parentIsVisible, parent[name]);
            } else {
                parent[name] = 1
                // console.log(spaces.join('') + item.name);
            }
        }
    };

    return function _getStructure(psd) {
        const psd_tree_export = psd.tree().export();
        json = {};
        walkTreeExport(psd_tree_export.children);
        // console.log(JSON.stringify(json, null, '    '));
        return json;
    };
}());

module.exports = {
    getDetailVisibleNodes,
    getVisibleNodes,
    getStructure,
    getLayerNodes
};
// getStructure(psd)
